package mainFragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.apmem.tools.layouts.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import mainFragment.bean.HotCollegeBean;
import view.MyImageLoader;

/**
 * Created by Administrator on 2017/2/22.
 */

public class RMGXLvAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<HotCollegeBean.CollegeListBean> arrayList;
    private static final String TAG = "RMGXLvAdapter";

    public RMGXLvAdapter(Context context, ArrayList<HotCollegeBean.CollegeListBean> arrayList) {
        this.context = context;
        this.arrayList = arrayList;

    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;
        if (convertView == null) {
            myHolder = new MyHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.rmgx_lv_item, null);
            myHolder.rmgx_icon = (ImageView) convertView.findViewById(R.id.rmgx_icon);
            myHolder.rmgx_lv_item_college_name = (TextView) convertView.findViewById(R.id.rmgx_lv_item_college_name);
            myHolder.wangnianzuidifen_wen = (TextView) convertView.findViewById(R.id.wangnianzuidifen_wen);
            myHolder.wangnianzuidifen_li = (TextView) convertView.findViewById(R.id.wangnianzuidifen_li);
            myHolder.rmgx_flowLayout = (FlowLayout) convertView.findViewById(R.id.rmgx_flowLayout);


            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }


        List<String> tag = arrayList.get(position).getTag();

        if (myHolder.rmgx_flowLayout.getChildCount()==0) {
            for (int i = 0; i < tag.size(); i++) {
                View relativeLayout = LayoutInflater.from(context).inflate(R.layout.college_tag, null);
                TextView textView = (TextView) relativeLayout.findViewById(R.id.rmgx_lv_item_tag);
                textView.setText(tag.get(i));
                myHolder.rmgx_flowLayout.addView(relativeLayout);
            }
        }
        else{
            myHolder.rmgx_flowLayout.removeAllViews();
            for (int i = 0; i < tag.size(); i++) {
                View relativeLayout = LayoutInflater.from(context).inflate(R.layout.college_tag, null);
                TextView textView = (TextView) relativeLayout.findViewById(R.id.rmgx_lv_item_tag);
                textView.setText(tag.get(i));
                myHolder.rmgx_flowLayout.addView(relativeLayout);
            }
        }
        MyImageLoader.showImage(arrayList.get(position).getIcon(), myHolder.rmgx_icon);
        myHolder.rmgx_lv_item_college_name.setText(arrayList.get(position).getSchool_name());
        myHolder.wangnianzuidifen_wen.setText(arrayList.get(position).getLowest_art());
        myHolder.wangnianzuidifen_li.setText(arrayList.get(position).getLowest_science());

        return convertView;
    }

    class MyHolder {
        FlowLayout rmgx_flowLayout;
        ImageView rmgx_icon;
        TextView rmgx_lv_item_college_name;
        TextView wangnianzuidifen_wen;
        TextView wangnianzuidifen_li;
    }
}
